<?php
namespace app\yunda\home;

use app\yunda\model\Goods as GoodsModel;
use app\yunda\model\User;
use app\yunda\model\ScoreRecord;
use app\yunda\model\Goods_type;
use app\yunda\model\Score_order;
use think\Db;
/**
 * 积分商城
 * @package app\yunda\home
 */
class Shop extends Base {
    // 获取列表
    public function index(){
        // 查询字段
        $map = $this->getMap();
        $map['g.status']=1;
        $Post = new GoodsModel();
        //查询所有积分商品分类
        $category=Goods_type::get_list_goodType('home');

        foreach ($category as &$v){
            $where=['status'=>1,'good_type'=>$v['id']];
            $order='sort desc,id desc';
            $v['show']=GoodsModel::where($where)->order($order)->paginate()->each(function ($item){
                $item['good_img']=get_images_all($item['good_img']);
                return $item;
            });
        }
        $data = $category->toArray();
        return mogo_json($data);
    }


    //积分兑换
    public function score_change()
    {
        $data=request()->only(['openid','id']);
        if(empty($data['openid'])) return mogo_error('参数错误',404);
        if(empty($data['id'])) return mogo_error('参数错误',404);
        //判断用户是否存在
        $user=User::get(['openid'=>$data['openid']]);
        if(empty($user))return mogo_error('用户不存在',0);
        //判断兑换商品是否存在
        $good=GoodsModel::get($data['id']);
        if(empty($good)) return mogo_error('该商品已经下架');
        if($good['stock']<=0)return mogo_error('该商品库存不足',0);
        //扣除用户积分兑换商品
        $user['score']-=$good['need_score'];
        if($user['score']<0) return mogo_error('您积分余额不足',0);
        //判断用户是否填写个人信息邮编，地区，地址，如没有则需设置
        if(empty($user['post_code']||$user['home']||$user['address']))
            return mogo_error('请您去完善个人信息',2);
        //加入积分兑换订单
        $order=Score_order::create([
            'openid'=>$data['openid'],
            'good_id'=>$data['id'],
            'order_type'=>1,//积分兑换
        ]);
        $res=User::update([
           'id'=>$user['id'],
           'score'=>$user['score'],
        ]);
        //扣除积分成功,确认兑换成功
        Score_order::update(['id'=>$order['id'],'is_pay'=>1]);
        $recharge='兑换'.$good['good_name'].'消耗积分'.$good['need_score'];
        $good['need_score']='-'.$good['need_score'];
        ScoreRecord::createScoreRecord($recharge,$good['need_score'],$user['id']);
        //扣除商品库存
        $good['stock']-=1;
        GoodsModel::update([
            'id'=>$good['id'],
            'stock'=>$good['stock'],
        ]);
        //消耗积分获得抽奖次数
        return mogo_json($res,'兑换成功,请联系客服确认发货',1);
    }

    //我的兑换
    public function mychange($openid=''){
        if(empty($openid))return mogo_error('参数错误',404);
        // 查询字段
        $map = $this->getMap();
        $Address = new Score_order;
        $map['yd_score_order.delete_time']=NULL;
        $map['yd_score_order.order_type']=1;//积分订单
        $map['yd_score_order.openid']=$openid;
        // 自定义where查询
        $data = Db::view('yd_score_order',true)
            ->view('yd_user',['name','post_code','home','address','phone'],'yd_user.openid=yd_score_order.openid','left')
            ->view('yd_score_goods','good_name,good_img','yd_score_goods.id=yd_score_order.good_id','left')
            ->where($map)
            ->order('create_time desc')
            ->paginate()->each(function($item){
                $item['address1']=$item['home'].$item['address'];
                $arr=['未兑换','已兑换'];
                $item['is_pay']=$arr[$item['is_pay']];
                $item['create_time']=format_time($item['create_time']);
                $item['good_img']=get_images_all($item['good_img']);
                return $item;
            });
        $data = $data->toArray();
        return mogo_json($data,'',1);
    }

    public function detail($id='')
    {
        if(empty($id))return mogo_error('参数错误',404);
        $row=GoodsModel::get($id);
        if(empty($row))return mogo_error('商品不存在',404);
        $row['good_img']=get_images_all($row['good_img']);
        return mogo_json($row,'',1);
    }
}